import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  },
]

export const asyncRoutes = [
  {
    path: '/table',
    component: Layout,
    redirect: '/table',
    name: 'Table',
    meta: {
      title: '用户管理',
      icon: 'table'
    },
    children: [
      {
        path: 'user',
        component: () => import('@/views/table/user'),
        name: 'User',
        meta: { title: '后台用户管理' }
      },
      {
        path: 'polling',
        component: () => import('@/views/table/polling'),
        name: 'Polling',
        meta: { title: '巡检人员管理' }
      }
    ]
  },

  {
    path: '/example',
    component: Layout,
    redirect: '/example/list',
    name: 'Example',
    meta: {
      title: '任务编辑',
      icon: 'el-icon-s-help'
    },
    children: [
      {
        path: 'module',
        component: () => import('@/views/task/task-module'),
        name: 'Module',
        meta: { title: '任务模块' }
      },
      {
        path: 'temp',
        component: () => import('@/views/task/task-temp'),
        name: 'Temp',
        meta: { title: '任务模板' }
      },
      {
        path: 'plan',
        component: () => import('@/views/task/task-plan'),
        name: 'Plan',
        meta: { title: '任务计划' }
      },
      // {
      //   path: 'site',
      //   component: () => import('@/views/task/task-site'),
      //   name: 'Site',
      //   meta: { title: '任务计划站址' }
      // }
    ]
  },
  {
    path: '/release',
    component: Layout,
    meta: {
      title: '任务发布',
      icon: 'tab'
    },
    children: [
      {
        path: 'planRelease',
        component: () => import('@/views/task/task-release'),
        name: 'PlanRelease',
        meta: { title: '任务计划发布' }
      }, {
        path: 'inspect',
        component: () => import('@/views/task/task-inspect'),
        name: 'Inspect',
        meta: { title: '巡检子任务' }
      }
    ]
  }, {
    path: '/taskAudit',
    component: Layout,
    meta: {
      title: '任务审核统计',
      icon: 'tab'
    },
    children: [
      {
        path: 'subtaskApproval',
        component: () => import('@/views/task/task-audit'),
        name: 'SubtaskApproval',
        meta: { title: '巡检子任务审核' }
      }, {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '任务统计' }
      }
    ]
  },
  {
    path: '/site',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/site/site-list'),
        name: 'Site',
        meta: { title: '站点管理', icon: 'nested' }
      }
    ]
  },
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
